// Author: Uberteg and Civic TSI

num hBoostFuel			= -1;			// variables to store window handles
num hBoostIgn			= -1;			//

num h175Map			= -1;
num h200Map			= -1;
num h250Map			= -1;
num h300Map			= -1;
num h350Map			= -1;

num BOOST_FUEL_FLOAT	= 0x7A00;		// location to save editor used variable for boost fuel efficiency
num BOOST_IGN_FLOAT		= 0x7A04;		// location to save editor used variable for ign retard per lbs

void open()
{

	num x = 10;
	num y = 10;

	CreateBorder(x, y, 250, 80, 4, 0, "Tables");
	CreateStatic(x+5, y-5, 150, 20, 4, 0, "Boost Table Funcs");

	CreateStatic(120+x+10, y+20, 110, 20, 4, 0, "Boost Fuel Eff");
	CreateStatic(120+x+10, y+40, 110, 20, 4, 0, "Boost Ign Retard");

	hBoostFuel = CreateEdit(x+10, y+20, 40, 20, 4, 0, "Boost Fuel Eff");
	hBoostIgn  = CreateEdit(x+10, y+40, 40, 20, 4, 0, "Boost Ign Retard");

	CreateBtn(40+x+10, y+20, 50, 20, 4, "BoostFuelApply", "Apply");
	CreateBtn(40+x+10, y+40, 50, 20, 4, "BoostIgnRetardApply", "Apply");
//	CreateBtn(40+x+10, y+40, 50, 20, 4, "BoostIgnApply", "Apply");

	y = y + 95;
	CreateBorder(x, y, 250, 140, 4, 0, "MapSensor");
	CreateStatic(x+5, y-5, 150, 20, 4, 0, "Map Sensor");

	h175Map = CreateRadio(x+10, y+20, 70, 20, 4, "convert175bar", "1.75 Bar");
	h200Map = CreateRadio(x+10, y+40, 70, 20, 4, "convert2bar", "2.00 Bar");
	h250Map = CreateRadio(x+10, y+60, 70, 20, 4, "convert25bar", "2.50 Bar");
	h300Map = CreateRadio(x+10, y+80, 70, 20, 4, "convert3bar", "3.00 Bar");
	h350Map = CreateRadio(x+10, y+100, 70, 20, 4, "convert35bar", "3.50 Bar");

	setcurmap();
	SetBoostItems();
}

void DisableCRC()
{
	writefloat(BOOST_FUEL_FLOAT, 120);
	writefloat(BOOST_IGN_FLOAT, 0.9);

	writefloat(MAPSENSOR_FLOAT, 1.75/2)
	convert175bar();
}

void SetBoostItems()
{
	SetItem(hBoostFuel, readfloat(BOOST_FUEL_FLOAT));
	SetItem(hBoostIgn, readfloat(BOOST_IGN_FLOAT));
}

num BoostFuelApply()						// callback for "Boost Fuel Eff" window
{
	num mult = GetItem(hBoostFuel);
	SetBoostFuelMaps(mult);
	writefloat(BOOST_FUEL_FLOAT, mult);
}

num BoostIgnApply()							// callback for "Boost Ign Retard" window
{
	num mult = GetItem(hBoostIgn);
	SetIgnitionRetard(mult);
	writefloat(BOOST_IGN_FLOAT, mult);
}

void BoostIgnRetardApply()
{
	num mult = GetItem(hBoostIgn);
	writefloat(BOOST_IGN_FLOAT, mult);
	BoostIgnRetard(IGN_BOOST_TABLE, NEW_TABLELO_X, NEW_TABLELO_Y);
	BoostIgnRetard(IGNHI_BOOST_TABLE, NEW_TABLEHI_X, NEW_TABLEHI_Y);
	PopulateTables();
}

void BoostIgnRetard(num Table, num dx, num dy)
{
	num x;
	num y;
	num psi;
	num ign;
	num newign;
	num nbretard;
	num	xstart;
	num src_addr;
	num dest_addr;

	x = 0;
	xstart = 0;
	while(x < dx)
	{
		psi = MBar2InHgPsi(MBAR_TABLE, x);
		if(psi < 0)
		{
			xstart = x;
			x = x + 1;
		}
		else
		{
			x = x + dx;
		}
	}

	y = 0;
	nbretard = GetItem(hBoostIgn) * 4;
	while(y < dy)
	{
		src_addr = Table + (y * dx) + xstart;
		ign = readbyte(src_addr);
		x = xstart + 1;
		while(x < dx)
		{
			dest_addr = Table + (y * dx) + x;
			psi = MBar2InHgPsi(MBAR_TABLE, x);
			newign = ign - (psi * nbretard);
			if(newign < 0)
				newign = 0;
			writebyte(dest_addr, newign);
			x = x + 1;
		}
		y = y + 1;
	}
}

void setcurmap()
{
	num mapsensor = readfloat(MAPSENSOR_FLOAT);

	SetItem(h175Map, 0);
	SetItem(h200Map, 0);
	SetItem(h250Map, 0);
	SetItem(h300Map, 0);
	SetItem(h350Map, 0);

	if(mapsensor == 1.75)
	{
		SetItem(h175Map, 1);
		return;
	}
	if(mapsensor == 2)
	{
		SetItem(h200Map, 1);
		return;
	}
	if(mapsensor == 2.5)
	{
		SetItem(h250Map, 1);
		return;
	}
	if(mapsensor == 3)
	{
		SetItem(h300Map, 1);
		return;
	}
	if(mapsensor == 3.5)
	{
		SetItem(h350Map, 1);
		return;
	}
}

void convert175bar()								// converts rom to 2bar rom
{
	writebyte(0x084B, 0xB);
	writebyte(0x4DCC, 0x18);
	writebyte(0x4DD2, 1);
	scalemap(MBAR_TABLE, (readfloat(MAPSENSOR_FLOAT) / 1.75), 10);
	writefloat(MAPSENSOR_FLOAT, 1.75);
	genextramap();
	setcurmap();
	ReadMapScalar();
}

void convert2bar()								// converts rom to 2bar rom
{
	writebyte(0x084B, 0x8);
	writebyte(0x4DCC, 0x15);
	writebyte(0x4DD2, 1);
	scalemap(MBAR_TABLE, (readfloat(MAPSENSOR_FLOAT) / 2), 10);
	writefloat(MAPSENSOR_FLOAT, 2);
	genextramap();
	setcurmap();
	ReadMapScalar();
}

void convert25bar()								// converts rom to 2bar rom
{
	writebyte(0x084B, 0x6);
	writebyte(0x4DCC, 0x10);
	writebyte(0x4DD2, 1);
	scalemap(MBAR_TABLE, (readfloat(MAPSENSOR_FLOAT) / 2.5), 10);
	writefloat(MAPSENSOR_FLOAT, 2.5);
	genextramap();
	setcurmap();
	ReadMapScalar();
}

void convert3bar()								// converts rom to 3bar rom
{
	writebyte(0x084B, 0x4);
	writebyte(0x4DCC, 0xE);
	writebyte(0x4DD2, 1);
	scalemap(MBAR_TABLE, (readfloat(MAPSENSOR_FLOAT) / 3), 10);
	writefloat(MAPSENSOR_FLOAT, 3);
	genextramap();
	setcurmap();
	ReadMapScalar();
}

void convert35bar()								// converts rom to 3bar rom
{
	writebyte(0x084B, 0x4);
	writebyte(0x4DCC, 0xC);
	writebyte(0x4DD2, 1);
	scalemap(MBAR_TABLE, (readfloat(MAPSENSOR_FLOAT) / 3.5), 10);
	writefloat(MAPSENSOR_FLOAT, 3.5);
	genextramap();
	setcurmap();
	ReadMapScalar();
}

num scalemap(num map, num scale, num len)		// scales map table by the specified amount
{
	num x;

	x = readbyte(map+0) * scale;
	writebyte(map+0, x);
	x = readbyte(map+1) * scale;
	writebyte(map+1, x);
	x = readbyte(map+2) * scale;
	writebyte(map+2, x);
	x = readbyte(map+3) * scale;
	writebyte(map+3, x);
	x = readbyte(map+4) * scale;
	writebyte(map+4, x);
	x = readbyte(map+5) * scale;
	writebyte(map+5, x);
	x = readbyte(map+6) * scale;
	writebyte(map+6, x);
	x = readbyte(map+7) * scale;
	writebyte(map+7, x);
	x = readbyte(map+8) * scale;
	writebyte(map+8, x);
	x = readbyte(map+9) * scale;
	writebyte(map+9, x);
}

void genextramap()								// generates new map table values for the extra portion of the map
{
	num i;
	i = 10;
	while(i < NEW_TABLELO_X-1)
	{
		num x = (i - 10) + 1;
		num xmax = (NEW_TABLELO_X - 1) - 10;
		num scale = x / xmax;
		num min = LAST_MAP_VALUE * 1.75 / readfloat(MAPSENSOR_FLOAT);
		num max = 225.0f;
		num final = (scale * (max-min))+min;
		if(final > 255)
		{
			final = 255;
		}
		writebyte(MBAR_TABLE + i, final);
		i = i + 1;
	}
	writebyte(MBAR_TABLE + i, 0);
	writebyte(MBAR_TABLE + 9, LAST_MAP_VALUE * 1.75 / readfloat(MAPSENSOR_FLOAT));
}

num getmapsen()									// retrieves current map sensor, called outside of script
{
	return readfloat(MAPSENSOR_FLOAT);
}

